<template>
  <div class="login-container">
    <div class="login-bg"></div>
    <div class="login-card">
      <div class="login-logo">
        <div class="logo-icon"></div>
        <h3 class="title">{{title}}</h3>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
            :class="{'input-focus': usernameFocus}"
            @focus="usernameFocus = true"
            @blur="usernameFocus = false"
          >
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            :class="{'input-focus': passwordFocus}"
            @focus="passwordFocus = true"
            @blur="passwordFocus = false"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaEnabled">
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%"
            :class="{'input-focus': codeFocus}"
            @focus="codeFocus = true"
            @blur="codeFocus = false"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img"/>
          </div>
        </el-form-item>
        <div class="form-footer">
          <el-checkbox v-model="loginForm.rememberMe" class="remember-me">记住密码</el-checkbox>
          <div v-if="register" class="register-link">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>
        </div>
        <el-form-item style="width:100%; margin-top: 20px;">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width:100%;"
            @click.native.prevent="handleLogin"
            class="login-btn"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2025 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      title: process.env.VUE_APP_TITLE,
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
      // 输入框聚焦状态
      usernameFocus: false,
      passwordFocus: false,
      codeFocus: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
    // 添加页面加载动画
    document.body.classList.add('login-loading')
    setTimeout(() => {
      document.body.classList.remove('login-loading')
    }, 500)
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get("username")
      const password = Cookies.get("password")
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 })
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove("username")
            Cookies.remove("password")
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
/* 全局样式 */
body.login-loading {
  overflow: hidden;
}

.login-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  position: relative;
  overflow: hidden;
}

.login-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url("../assets/images/login3.jpg");
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
  filter: brightness(0.85);
  z-index: 1;
  transform: scale(1.02);
  animation: bgZoom 20s infinite alternate ease-in-out;
}

.login-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
  width: 420px;
  padding: 30px;
  z-index: 2;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  transform: translateY(0);
  animation: cardFloat 6s ease-in-out infinite;
}

.login-logo {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 30px;
}

.logo-icon {
  width: 60px;
  height: 60px;
  background-color: #409EFF;
  border-radius: 50%;
  margin-bottom: 15px;
  position: relative;
  overflow: hidden;
}

.logo-icon::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 30px;
  height: 30px;
  background-color: white;
  clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
}

.title {
  margin: 0px auto 10px auto;
  text-align: center;
  color: #303133;
  font-size: 24px;
  font-weight: 600;
}

.login-form {
  width: 100%;
  padding: 0;
  .el-input {
    height: 42px;
    margin-bottom: 20px;
    transition: all 0.3s ease;
    border-radius: 6px;
    input {
      height: 42px;
      border-radius: 6px;
      border-color: #dcdfe6;
      transition: all 0.3s ease;
    }
    &.input-focus {
      input {
        border-color: #409EFF;
        box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
      }
    }
  }
  .input-icon {
    height: 42px;
    width: 16px;
    margin-left: 5px;
    color: #c0c4cc;
  }
}

.form-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.remember-me {
  font-size: 14px;
  color: #606266;
  .el-checkbox__label {
    color: #606266;
  }
}

.register-link {
  font-size: 14px;
}

.link-type {
  color: #409EFF;
  text-decoration: none;
  transition: color 0.2s;
  &:hover {
    color: #66b1ff;
  }
}

.login-btn {
  height: 44px;
  line-height: 44px;
  font-size: 16px;
  border-radius: 22px;
  background-color: #409EFF;
  border: none;
  transition: all 0.3s ease;
  &:hover {
    background-color: #66b1ff;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
  }
  &:active {
    transform: translateY(0);
  }
}

.login-code {
  width: 33%;
  height: 42px;
  float: right;
  border-radius: 6px;
  overflow: hidden;
}

.login-code-img {
  width: 100%;
  height: 100%;
  cursor: pointer;
  transition: transform 0.3s ease;
  &:hover {
    transform: scale(1.05);
  }
}

.el-login-footer {
  position: absolute;
  bottom: 20px;
  left: 0;
  width: 100%;
  text-align: center;
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  z-index: 2;
}

/* 动画效果 */
@keyframes bgZoom {
  0% {
    transform: scale(1.02);
  }
  100% {
    transform: scale(1.05);
  }
}

@keyframes cardFloat {
  0% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-10px);
  }
  100% {
    transform: translateY(0px);
  }
}

/* 表单验证样式优化 */
.el-form-item {
  margin-bottom: 15px;
}

.el-form-item__error {
  font-size: 12px;
  padding-top: 4px;
}
</style>  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
